package com.syh.jn.scm.infrastructure.repository.impl.operation.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.core.expction.DataModelException;
import com.syh.jn.scm.domain.operation.entity.system.DictType;
import com.syh.jn.scm.domain.operation.repository.system.DictTypeRepository;
import com.syh.jn.scm.infrastructure.repository.model.base.DictTypeModel;
import org.springframework.stereotype.Repository;

/**
 * Date:2018/7/12
 * Author:Clown
 * 运营平台字典类型仓储实现;
 */
@Repository
public class DictTypeRepositoryImpl extends AbstractAggregateRepository<DictType, DictTypeModel, Long> implements DictTypeRepository{
  @Override
  public Long add(DictType dictType){
    ExtAssert.notNull(dictType,"对象不存在");

    if (!Long.valueOf("0").equals(dictType.getSupId())) {
      DictTypeModel father = super.hibernateTemplate.get(DictTypeModel.class,dictType.getSupId());
      if (father == null) {
        throw new DataModelException("父级类型不存在");
      }
    }
    DictTypeModel categoryModel = new DictTypeModel();
    ExtBeanUtils.copyProperties(dictType,categoryModel);
    dictType.setId((Long) this.hibernateTemplate.save(categoryModel));
    return dictType.getId();
  }

  @Override
  public void update(DictType dictType){
    ExtAssert.notNull(dictType,"对象不存在");

    if (!Long.valueOf("0").equals(dictType.getSupId())) {
      DictTypeModel father = super.hibernateTemplate.get(DictTypeModel.class,dictType.getSupId());
      if (father == null) {
        throw new DataModelException("父级类型不存在");
      }
    }
    DictTypeModel categoryModel = this.hibernateTemplate.get(DictTypeModel.class,dictType.getId());
    ExtBeanUtils.copyProperties(dictType,categoryModel);
    this.hibernateTemplate.update(categoryModel);
  }
}
